package com.borball.store.domain;

import java.io.Serializable;
import java.sql.Timestamp;

public class ProductOut implements Serializable {

	private static final long serialVersionUID = -949430328440379285L;
	private int id;
	private String user;
	private String status;
	private Timestamp created;
	private String products;
	private String returnUnits;
	private String memo;

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Timestamp getCreated() {
		return created;
	}

	public void setCreated(Timestamp created) {
		this.created = created;
	}

	public String getProducts() {
		return products;
	}

	public void setProducts(String products) {
		this.products = products;
	}

	public String getReturnUnits() {
		return returnUnits;
	}

	public void setReturnUnits(String returnUnits) {
		this.returnUnits = returnUnits;
	}

	public UnitAmount[] string2UnitAmount() {
		String[] composites = returnUnits.split("\\|");
		UnitAmount[] uas = new UnitAmount[composites.length];

		int i = 0;
		for (String c : composites) {
			String[] kv = c.split("\\:");
			UnitAmount uai = new UnitAmount();
			uai.setName(kv[0]);

			if(kv.length==1){
				uai.setAmount(0);
			} else {
				uai.setAmount(Integer.valueOf(kv[1]));
			}
			uas[i] = uai;
			i++;
		}

		return uas;
	}

	public String unitAmount2String(UnitAmount[] uas) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < uas.length; i++) {
			sb.append(uas[i].getName()).append(":").append(uas[i].getAmount()).append("|");
		}

		return sb.toString().substring(0, sb.length() - 1);
	}
	
	public ProductAmount[] string2ProductAmount() {
		String[] composites = products.split("\\|");
		ProductAmount[] uas = new ProductAmount[composites.length];

		int i = 0;
		for (String c : composites) {
			String[] kv = c.split("\\:");
			ProductAmount uai = new ProductAmount();
			uai.setName(kv[0]);

			if(kv.length==1){
				uai.setAmount(0);
			} else {
				uai.setAmount(Integer.valueOf(kv[1]));
			}
			uas[i] = uai;
			i++;
		}

		return uas;
	}

	public String productAmount2String(ProductAmount[] uas) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < uas.length; i++) {
			sb.append(uas[i].getName()).append(":").append(uas[i].getAmount()).append("|");
		}

		return sb.toString().substring(0, sb.length() - 1);
	}
	
}
